package com.bening.smsapp.main;

import com.bening.smsapp.bootstrap.BootConfigParameters;
import com.bening.smsapp.constant.SmsAppConstant;
import com.bening.smsapp.downloader.FtpDownloaderDaemon;
import com.bening.smsapp.factory.*;
import com.bening.smsapp.filechangereg.FileChangeRegDaemon;

import java.sql.*;
import com.bening.smsapp.parser.*;

public class Smsapp
{ 
	
   
   
    public static void main( String[] args )
    {
    	String fileConfigPath = "";
        if( args.length == 0 )
        {	
            displayHelp();
        }
        
        else if(args.length == 1) {
        	fileConfigPath = System.getProperty(SmsAppConstant.CORE_CONFIGPATH); 
        	BootConfigParameters bootP = BootConfigParameters.getInstance(fileConfigPath);
        	if(args[0].equals(SmsAppConstant.CORE_PARSER)) {
        		ParserDaemon.main(args);
        	}else if(args[0].equals(SmsAppConstant.CORE_DOWNLOADER)) {
        		FtpDownloaderDaemon.main(args);
        	}else if(args[0].equals(SmsAppConstant.CORE_CHECKER)) {
        		FileChangeRegDaemon.main(args);
        	}else if(args[0].equals(SmsAppConstant.CORE_TESTDB)) {
        		testDatabaseConnection();
        	}else {
        		displayUnknownCommand();
        	}
        }
        else {
        	displayHelp();
        }
    }

    private static void displayHelp()
    {
        System.out.println( "\nRun:\n\njava -jar SmsApp.jar config.file=filename [ command ] ]" );
        System.out.println( "Commands:\n" );
        System.out.println( "     help        		- display this help text" );
        System.out.println( "     testdb          		- test database connection" );
        System.out.println( "     check		   		- start file change registration service" );
        System.out.println( "     download  		- start ftp downloader service" );
        System.out.println( "     parse 			- start parser service" );
    }

    private static void testDatabaseConnection()
    {
        System.out.println( "test database connection." );
        PreparedStatement prep = null;
        Connection conn = SmsAppFactory.getDbConnection();
        ResultSet rs = null;
        try {
        	prep = conn.prepareStatement("SELECT * FROM TAB");
        	rs = prep.executeQuery();
        	if(rs.next()) {
        		System.out.println("koneksi database berhasil ...");
        	}else {
        		System.out.println("koneksi database gagal....");
        	}
        	rs.close();
        	prep.close();
        	conn.close();
        	
        }catch(SQLException se) {
        	se.printStackTrace();
        }
        
    }

    private static void displayUnknownCommand()
    {
        System.out.println( "Unknown command." );
    }
}


